```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>秒杀系统架构设计 | 高性能架构解密</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        :root {
            --primary: #2563eb;
            --primary-dark: #1d4ed8;
            --secondary: #e11d48;
            --accent: #10b981;
            --dark: #1e293b;
            --light: #f8fafc;
        }
        body {
            font-family: 'Noto Sans SC', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif;
            color: var(--dark);
            line-height: 1.6;
            background-color: #f8fafc;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', Georgia, 'Times New Roman', Times, serif;
            font-weight: 600;
            color: var(--dark);
        }
        .hero {
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            color: white;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .feature-icon {
            background-color: rgba(37, 99, 235, 0.1);
            color: var(--primary);
        }
        .code-block {
            background-color: rgba(30, 41, 59, 0.05);
            border-left: 4px solid var(--primary);
        }
        .divider {
            border: none;
            height: 1px;
            background: linear-gradient(90deg, transparent, rgba(0,0,0,0.1), transparent);
        }
        .footer {
            background-color: var(--dark);
            color: white;
        }
    </style>
</head>
<body>
    <!-- Navigation -->
    <nav class="bg-white shadow-sm sticky top-0 z-50">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between h-16">
                <div class="flex items-center">
                    <span class="text-xl font-bold text-gray-900">秒杀系统架构</span>
                </div>
                <div class="hidden md:flex items-center space-x-8">
                    <a href="#overview" class="text-gray-700 hover:text-primary-600 font-medium">概述</a>
                    <a href="#features" class="text-gray-700 hover:text-primary-600 font-medium">核心特性</a>
                    <a href="#solution" class="text-gray-700 hover:text-primary-600 font-medium">解决方案</a>
                    <a href="#design" class="text-gray-700 hover:text-primary-600 font-medium">设计理念</a>
                    <a href="#implementation" class="text-gray-700 hover:text-primary-600 font-medium">实现细节</a>
                </div>
            </div>
        </div>
    </nav>

    <!-- Hero Section -->
    <section class="hero py-20 md:py-32">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6">秒杀系统架构设计</h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto">
                    高性能、高一致、高可用的三高系统解决方案
                </p>
                <div class="mt-10">
                    <a href="#overview" class="inline-flex items-center px-6 py-3 border border-transparent text-base font-medium rounded-md shadow-sm text-primary-700 bg-white hover:bg-gray-50 transition-colors duration-200">
                        <i class="fas fa-arrow-down mr-2"></i> 了解更多
                    </a>
                </div>
            </div>
        </div>
    </section>

    <!-- Overview Section -->
    <section id="overview" class="py-16">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="text-center mb-12">
                <h2 class="text-3xl font-bold mb-4">秒杀系统介绍</h2>
                <div class="w-24 h-1 bg-primary-600 mx-auto"></div>
            </div>
            
            <div class="bg-white rounded-lg shadow-md p-8 mb-12">
                <blockquote class="text-xl italic text-gray-700 border-l-4 border-primary-600 pl-6 py-2">
                    秒杀无论是双十一购物还是 12306 抢票，秒杀场景已随处可见。简单来说，秒杀就是在同一时刻大量请求争抢购买同一商品并完成交易的过程。
                </blockquote>
                <p class="mt-6 text-lg text-gray-700">
                    从架构视角来看，秒杀系统本质是一个<strong class="text-primary-600">高性能、高一致、高可用</strong>的三高系统。通俗一点讲就是网络商家为促销等目的组织的网上限时抢购活动。
                </p>
            </div>

            <div class="grid md:grid-cols-3 gap-8">
                <div class="card bg-white rounded-lg p-6">
                    <div class="feature-icon w-16 h-16 rounded-full flex items-center justify-center mb-4">
                        <i class="fas fa-bolt text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">高并发</h3>
                    <p class="text-gray-600">秒杀的特点就是时间极短、瞬间用户量大，系统需要承受极高的并发压力。</p>
                </div>
                <div class="card bg-white rounded-lg p-6">
                    <div class="feature-icon w-16 h-16 rounded-full flex items-center justify-center mb-4">
                        <i class="fas fa-boxes text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">库存量少</h3>
                    <p class="text-gray-600">一般秒杀活动商品量很少，这就导致了只有极少量用户能成功购买到。</p>
                </div>
                <div class="card bg-white rounded-lg p-6">
                    <div class="feature-icon w-16 h-16 rounded-full flex items-center justify-center mb-4">
                        <i class="fas fa-shield-alt text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">安全挑战</h3>
                    <p class="text-gray-600">需要应对恶意请求，防止系统被攻击，同时保证数据库压力可控。</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Features Section -->
    <section id="features" class="py-16 bg-gray-50">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="text-center mb-12">
                <h2 class="text-3xl font-bold mb-4">秒杀系统特点</h2>
                <div class="w-24 h-1 bg-primary-600 mx-auto"></div>
            </div>

            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-lg shadow-md p-8">
                    <ul class="space-y-4">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span>高并发：秒杀的特点就是这样<strong>时间极短</strong>、 <strong>瞬间用户量大</strong></span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span>库存量少：一般秒杀活动商品量很少，这就导致了只有极少量用户能成功购买到</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span>业务简单：流程比较简单，一般都是下订单、扣库存、支付订单</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span>秒杀的商品不需要添加到购物车</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span>秒杀系统独立部署</span>
                        </li>
                    </ul>
                </div>
                <div class="bg-white rounded-lg shadow-md p-8">
                    <h3 class="text-xl font-bold mb-4">秒杀需要解决的问题</h3>
                    <div class="space-y-4">
                        <div class="flex items-start p-4 bg-red-50 rounded-lg">
                            <i class="fas fa-exclamation-triangle text-red-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-medium text-red-700">商品超卖问题</h4>
                                <p class="text-sm text-gray-600 mt-1">确保库存充足的情况下不会出现超卖</p>
                            </div>
                        </div>
                        <div class="flex items-start p-4 bg-blue-50 rounded-lg">
                            <i class="fas fa-network-wired text-blue-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-medium text-blue-700">高并发的处理</h4>
                                <p class="text-sm text-gray-600 mt-1">系统需要能够应对瞬时高并发请求</p>
                            </div>
                        </div>
                        <div class="flex items-start p-4 bg-purple-50 rounded-lg">
                            <i class="fas fa-database text-purple-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-medium text-purple-700">库存和订单一致性的问题</h4>
                                <p class="text-sm text-gray-600 mt-1">确保数据库中的数据一致性</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Solution Section -->
    <section id="solution" class="py-16">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="text-center mb-12">
                <h2 class="text-3xl font-bold mb-4">秒杀系统设计理念</h2>
                <div class="w-24 h-1 bg-primary-600 mx-auto"></div>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-lg shadow-md p-8">
                    <h3 class="text-xl font-bold mb-6 text-center">核心设计原则</h3>
                    <div class="space-y-6">
                        <div class="flex items-start">
                            <div class="flex-shrink-0">
                                <div class="flex items-center justify-center h-12 w-12 rounded-md bg-blue-100 text-blue-600">
                                    <i class="fas fa-filter"></i>
                                </div>
                            </div>
                            <div class="ml-4">
                                <h4 class="text-lg font-medium text-gray-900">限流</h4>
                                <p class="mt-1 text-gray-600">鉴于只有少部分用户能够秒杀成功，所以要限制大部分流量，只允许少部分流量进入服务后端</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="flex-shrink-0">
                                <div class="flex items-center justify-center h-12 w-12 rounded-md bg-green-100 text-green-600">
                                    <i class="fas fa-chart-line"></i>
                                </div>
                            </div>
                            <div class="ml-4">
                                <h4 class="text-lg font-medium text-gray-900">削峰</h4>
                                <p class="mt-1 text-gray-600">对于秒杀系统瞬时的大量用户涌入，所以在抢购开始会有很高的瞬时峰值。实现削峰的常用方法有利用缓存或消息中间件等技术</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="flex-shrink-0">
                                <div class="flex items-center justify-center h-12 w-12 rounded-md bg-purple-100 text-purple-600">
                                    <i class="fas fa-sync-alt"></i>
                                </div>
                            </div>
                            <div class="ml-4">
                                <h4 class="text-lg font-medium text-gray-900">异步处理</h4>
                                <p class="mt-1 text-gray-600">对于高并发系统，采用异步处理模式可以极大地提高系统并发量，异步处理就是削峰的一种实现方式</p>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-lg shadow-md p-8">
                    <h3 class="text-xl font-bold mb-6 text-center">关键技术</h3>
                    <div class="space-y-6">
                        <div class="flex items-start">
                            <div class="flex-shrink-0">
                                <div class="flex items-center justify-center h-12 w-12 rounded-md bg-red-100 text-red-600">
                                    <i class="fas fa-memory"></i>
                                </div>
                            </div>
                            <div class="ml-4">
                                <h4 class="text-lg font-medium text-gray-900">内存缓存</h4>
                                <p class="mt-1 text-gray-600">秒杀系统最大的瓶颈最终都可能会是数据库的读写，主要体现在的磁盘的 I/O，性能会很低，如果能把大部分的业务逻辑都搬到缓存来处理，效率会有极大的提升</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="flex-shrink-0">
                                <div class="flex items-center justify-center h-12 w-12 rounded-md bg-yellow-100 text-yellow-600">
                                    <i class="fas fa-expand"></i>
                                </div>
                            </div>
                            <div class="ml-4">
                                <h4 class="text-lg font-medium text-gray-900">可拓展</h4>
                                <p class="mt-1 text-gray-600">如果需要支持更多的用户或更大的并发，将系统设计为弹性可拓展的，如果流量来了，拓展机器就好</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="flex-shrink-0">
                                <div class="flex items-center justify-center h-12 w-12 rounded-md bg-indigo-100 text-indigo-600">
                                    <i class="fas fa-layer-group"></i>
                                </div>
                            </div>
                            <div class="ml-4">
                                <h4 class="text-lg font-medium text-gray-900">分层设计</h4>
                                <p class="mt-1 text-gray-600">系统采用分层架构设计，各层职责单一，便于扩展和维护</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-md p-8 mb-12">
                <h3 class="text-xl font-bold mb-6">常见秒杀场景</h3>
                <div class="grid sm:grid-cols-2 md:grid-cols-3 gap-6">
                    <div class="flex items-center p-4 bg-gray-50 rounded-lg">
                        <div class="flex-shrink-0 h-10 w-10 rounded-full bg-blue-100 text-blue-600 flex items-center justify-center mr-4">
                            <i class="fas fa-shopping-cart"></i>
                        </div>
                        <span class="font-medium">电商抢购限量商品</span>
                    </div>
                    <div class="flex items-center p-4 bg-gray-50 rounded-lg">
                        <div class="flex-shrink-0 h-10 w-10 rounded-full bg-green-100 text-green-600 flex items-center justify-center mr-4">
                            <i class="fas fa-train"></i>
                        </div>
                        <span class="font-medium">火车票抢座 12306</span>
                    </div>
                    <div class="flex items-center p-4 bg-gray-50 rounded-lg">
                        <div class="flex-shrink-0 h-10 w-10 rounded-full bg-purple-100 text-purple-600 flex items-center justify-center mr-4">
                            <i class="fas fa-ticket-alt"></i>
                        </div>
                        <span class="font-medium">演唱会门票抢购</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Database Design Section -->
    <section id="design" class="py-16 bg-gray-50">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="text-center mb-12">
                <h2 class="text-3xl font-bold mb-4">数据库设计</h2>
                <div class="w-24 h-1 bg-primary-600 mx-auto"></div>
            </div>

            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-lg shadow-md p-6">
                    <h3 class="text-xl font-bold mb-4 flex items-center">
                        <i class="fas fa-table mr-2 text-primary-600"></i> 商品表结构
                    </h3>
                    <div class="code-block p-4 rounded overflow-x-auto">
                        <pre><code class="text-sm">CREATE TABLE `t_goods` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `gname` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `gdesc` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `gprice` decimal(10,0) DEFAULT NULL,
  `gstock` int(10) DEFAULT NULL,
  `gpic` text COLLATE utf8_bin,
  `gtype` int(11) DEFAULT NULL,
  `version` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;</code></pre>
                    </div>
                </div>
                <div class="bg-white rounded-lg shadow-md p-6">
                    <h3 class="text-xl font-bold mb-4 flex items-center">
                        <i class="fas fa-table mr-2 text-primary-600"></i> 订单表结构
                    </h3>
                    <div class="code-block p-4 rounded overflow-x-auto">
                        <pre><code class="text-sm">CREATE TABLE `t_order` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `gid` int(10) DEFAULT NULL,
  `gname` varchar(30) COLLATE utf8_bin DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5335 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;</code></pre>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-md p-8 mt-12">
                <h3 class="text-xl font-bold mb-6">业务流程图</h3>
                <div class="bg-gray-50 p-6 rounded-lg">
                    <div class="mermaid">
                        graph TD
                            A[用户发起秒杀请求] --> B[检查秒杀是否开始]
                            B -->|是| C[检查库存]
                            B -->|否| Z[返回秒杀未开始]
                            C -->|库存充足| D[修改库存]
                            C -->|库存不足| Y[返回已售罄]
                            D --> E[创建订单]
                            E --> F[返回秒杀成功]
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Implementation Section -->
    <section id="implementation" class="py-16">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="text-center mb-12">
                <h2 class="text-3xl font-bold mb-4">技术实现细节</h2>
                <div class="w-24 h-1 bg-primary-600 mx-auto"></div>
            </div>

            <div class="bg-white rounded-lg shadow-md p-8 mb-12">
                <h3 class="text-xl font-bold mb-6">乐观锁解决超卖问题</h3>
                <div class="code-block p-4 rounded overflow-x-auto">
                    <pre><code class="text-sm">public interface IGoodsMpper {
    @Select("select * from t_goods where id = #{gid}")
    public Goods getGoodsById(Integer id);

    @Update("update t_goods set gstock = gstock - 1,version=version+1 
             where id = #{id} and version = #{version}")
    public int updateGstock(@Param("id") Integer id, @Param("version") Integer version);
}</code></pre>
                </div>
                <div class="mt-6 bg-blue-50 p-4 rounded-lg">
                    <h4 class="font-medium text-blue-800 mb-2">实现原理：</h4>
                    <p class="text-blue-700">通过版本号机制实现乐观锁，在更新库存时校验版本号，确保数据一致性。</p>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-md p-8 mb-12">
                <h3 class="text-xl font-bold mb-6">接口限流技术</h3>
                <p class="text-gray-700 mb-6">对某一时间窗口内的请求数进行限制，保持系统的可用性和稳定性，防止因流量暴增而导致的系统运行缓慢或宕机。</p>
                
                <div class="grid md:grid-cols-2 gap-8 mb-8">
                    <div>
                        <h4 class="text-lg font-bold mb-4 flex items-center">
                            <i class="fas fa-faucet mr-2 text-red-500"></i> 漏斗算法
                        </h4>
                        <img src="https://cdn.nlark.com/yuque/0/2025/png/21449790/1741350170328-ab42f897-70de-4e14-933e-04475f5e0db7.png" alt="漏斗算法图示" class="rounded-lg shadow-sm mb-4">
                        <p class="text-gray-600">把请求比作是水，水来了都先放进桶里，并以限定的速度出水，当水来得过猛而出水不够快时就会导致水直接溢出，即拒绝服务。</p>
                    </div>
                    <div>
                        <h4 class="text-lg font-bold mb-4 flex items-center">
                            <i class="fas fa-coins mr-2 text-yellow-500"></i> 令牌桶算法
                        </h4>
                        <img src="https://cdn.nlark.com/yuque/0/2025/png/21449790/1741350170396-a601cbb0-e3d4-48fa-80be-3176887ed387.png" alt="令牌桶算法图示" class="rounded-lg shadow-sm mb-4">
                        <p class="text-gray-600">令牌桶可自行以恒定的速率产生令牌。如果令牌不被消耗，令牌就会不断增多直到桶满。面对瞬时大流量，该算法可以在短时间内请求拿到大量令牌。</p>
                    </div>
                </div>

                <div class="mb-8">
                    <h4 class="text-lg font-bold mb-4">保护高并发系统的三把利器</h4>
                    <div class="grid md:grid-cols-3 gap-4">
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <h5 class="font-medium text-gray-800 mb-2">缓存</h5>
                            <p class="text-gray-600 text-sm">缓存的目的是提升系统访问速度和增大系统处理容量</p>
                        </div>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <h5 class="font-medium text-gray-800 mb-2">降级</h5>
                            <p class="text-gray-600 text-sm">降级是当服务器压力剧增的情况下，根据当前业务情况及流量对一些服务和页面有策略的降级</p>
                        </div>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <h5 class="font-medium text-gray-800 mb-2">限流</h5>
                            <p class="text-gray-600 text-sm">限流的目的是通过对并发访问/请求进行限速，或者对一个时间窗口内的请求进行限速来保护系统</p>
                        </div>
                    </div>
                </div>

                <div class="code-block p-4 rounded overflow-x-auto mb-6">
                    <h4 class="text-lg font-bold mb-4">Guava RateLimiter 实现</h4>
                    <pre><code class="text-sm">// 创建令牌桶实例, 每秒给桶中方10个令牌
private RateLimiter rateLimiter = RateLimiter.create(10);

@GetMapping("/testToken")
@ResponseBody
public String testToken() {
    // 获取令牌，该方法会被阻塞直到获取到请求
    double acquire = rateLimiter.acquire();

    // 尝试获取令牌,设置等待时间为1s
    if (rateLimiter.tryAcquire(1, TimeUnit.SECONDS)) {
        System.out.println("抢购成功");
        return "抢购成功。。。";
    } else {
        System.out.println("抢购失败");
        return "抢购失败。。。";
    }
}</code></pre>
                </div>
                
                <div class="code-block p-4 rounded overflow-x-auto">
                    <h4 class="text-lg font-bold mb-4">拦截器限流实现</h4>
                    <pre><code class="text-sm">@Component
public class LimitInterceptor implements HandlerInterceptor {
    private RateLimiter rateLimiter = RateLimiter.create(60);

    @Override
    public boolean preHandle(HttpServletRequest request, 
                           HttpServletResponse response, 
                           Object handler) throws Exception {
        // 获取到令牌往下执行
        if (rateLimiter.tryAcquire(1, TimeUnit.SECONDS)) {
            return true;
        } else {
            return false;
        }
    }
}

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private LimitInterceptor limitInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(limitInterceptor).addPathPatterns("/**");
    }
}</code></pre>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-md p-8 mb-12">
                <h3 class="text-xl font-bold mb-6">限时抢购实现</h3>
                <p class="text-gray-700 mb-6">利用Redis超时机制实现秒杀，秒杀开始后把seckilil_gid作为key,gid做为value，并设置超时时间，秒杀请求过来后先判断该key是否已进过期，如果已经过期说明秒杀已经结束。</p>
                
                <div class="grid md:grid-cols-2 gap-8 mb-8">
                    <div>
                        <h4 class="text-lg font-bold mb-4">秒杀时间控制</h4>
                        <img src="https://cdn.nlark.com/yuque/0/2025/png/21449790/1741350170451-481b7c5d-61a9-47be-92f1-9cbb628a9e31.png" alt="秒杀时间控制" class="rounded-lg shadow-sm mb-4">
                    </div>
                    <div>
                        <h4 class="text-lg font-bold mb-4">限制单用户频率</h4>
                        <div class="code-block p-4 rounded overflow-x-auto">
                            <pre><code class="text-sm">private boolean chekUserAccessLimit(Integer gid, Integer uid) {
    // 设置redis中的key
    String key = "seckill_" + uid + "_" + gid;

    if (!stringRedisTemplate.opsForValue()
        .setIfAbsent(key, "1", 60, TimeUnit.SECONDS)) {
        // 获取用户访问商品的次数
        synchronized (this){
            String num = stringRedisTemplate.opsForValue().get(key);
            if (Integer.parseInt(num) >= 10) {
                return false;
            }
            // 访问次数自增
            stringRedisTemplate.opsForValue().increment(key,1);
        }
    }
    return true;
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-md p-8">
                <h3 class="text-xl font-bold mb-6">削锋填谷技术</h3>
                <p class="text-gray-700 mb-6">某应用的处理能力是每秒 10 个请求。在某一秒，突然到来了 30 个请求，而接下来两秒，都没有请求到达。在这种情况下，如果直接拒绝 20 个请求，应用在接下来的两秒就会空闲。所以，需要把请求突刺均摊到一段时间内，让系统负载保持在请求处理水位之内，同时尽可能地处理更多请求，从而起到"削峰填谷"的效果。</p>
                
                <div class="code-block p-4 rounded overflow-x-auto">
                    <h4 class="text-lg font-bold mb-4">RabbitMQ 配置</h4>
                    <pre><code class="text-sm">spring:
  rabbitmq:
    host: 192.168.193.88
    port: 5672
    username: guest
    password: guest
    virtual-host: /
    listener:
      type: simple
      simple:
           prefetch: 10 # 消费者每次从队列获取的消息数量
        concurrency: 1 # 消费者数量
        max-concurrency: 1 # 启动消费者最大数量</code></pre>
                </div>
            </div>
        </div>
    </section>

    <!-- Summary Section -->
    <section class="py-16 bg-gray-50">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="bg-white rounded-lg shadow-md p-8">
                <h2 class="text-3xl font-bold mb-6">秒杀系统设计小结</h2>
                
                <div class="space-y-6">
                    <div class="p-6 bg-blue-50 rounded-lg">
                        <p class="text-blue-800 font-medium">秒杀系统就是一个"三高"系统，即<strong>高并发、高性能</strong>和<strong>高可用</strong>的分布式系统</p>
                    </div>
                    
                    <div class="p-6 bg-green-50 rounded-lg">
                        <h3 class="text-lg font-bold text-green-800 mb-3">秒杀设计原则</h3>
                        <ul class="space-y-2 text-green-700">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mt-1 mr-2"></i>
                                <span>前台请求尽量少</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mt-1 mr-2"></i>
                                <span>后台数据尽量少</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mt-1 mr-2"></i>
                                <span>调用链路尽量短</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mt-1 mr-2"></i>
                                <span>尽量不要有单点</span>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="p-6 bg-purple-50 rounded-lg">
                            <h3 class="text-lg font-bold text-purple-800 mb-3">秒杀高并发方法</h3>
                            <ul class="space-y-2 text-purple-700">
                                <li class="flex items-start">
                                    <i class="fas fa-arrow-right mt-1 mr-2"></i>
                                    <span>访问拦截</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-arrow-right mt-1 mr-2"></i>
                                    <span>分流</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-arrow-right mt-1 mr-2"></i>
                                    <span>动静分离</span>
                                </li>
                            </ul>
                        </div>
                        <div class="p-6 bg-yellow-50 rounded-lg">
                            <h3 class="text-lg font-bold text-yellow-800 mb-3">秒杀数据方法</h3>
                            <ul class="space-y-2 text-yellow-700">
                                <li class="flex items-start">
                                    <i class="fas fa-arrow-right mt-1 mr-2"></i>
                                    <span>减库存策略</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-arrow-right mt-1 mr-2"></i>
                                    <span>热点数据优化</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-arrow-right mt-1 mr-2"></i>
                                    <span>异步处理</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-arrow-right mt-1 mr-2"></i>
                                    <span>限流降级</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                    
                    <div class="p-6 bg-indigo-50 rounded-lg">
                        <h3 class="text-lg font-bold text-indigo-800 mb-2">什么是CDN?</h3>
                        <p class="text-indigo-700">CDN——Content Delivery Network，内容分发网络, 就是采用更多的缓存服务器（CDN边缘节点），布放在用户访问相对集中的地区或网络中。当用户访问网站时，利用全局负载技术，将用户的访问指向距离最近的缓存服务器上，由缓存服务器响应用户请求。</p>
                    </div>
                    
                    <div class="p-6 bg-pink-50 rounded-lg">
                        <h3 class="text-lg font-bold text-pink-800 mb-2">电商活动倒计时方案</h3>
                        <ol class="list-decimal list-inside space-y-2 text-pink-700">
                            <li>确定一个基准时间。可以使用一个sql语句从数据库中取出一个当前时间。SELECT NOW()；</li>
                            <li>活动开始的时间是固定的。</li>
                            <li>使用活动开始时间-基准时间可以计算出一个秒为单位的数值。</li>
                            <li>在redis中设置一个key（活动开始标识）。设置key的过期时间为第三步计算出来的时间。</li>
                            <li>展示页面的时候取出key的有效时间。Ttl命令。使用js倒计时。</li>
                            <li>一旦活动开始的key失效，说明活动开始。</li>
                            <li>需要在活动的逻辑中，先判断活动是否开始。</li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Footer -->
    <footer class="footer py-8">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <p class="text-white font-medium">技术小馆</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition-colors duration-200">
                        <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```